# -*- coding: utf-8 -*-
"""
Created on Fri Jan 15 22:06:36 2021

@author: 10365
"""

import json
import sqlGeneratorCore


def loadTableInfoFromJson(fileName):
    with open(fileName,"r",encoding="utf-8") as fp:
        jsonTable = json.load(fp)
    if jsonTable == None:
        return None
    tableName = jsonTable["tableName"]
    dbType = jsonTable["datebaseType"]
    tableInfo = sqlGeneratorCore.TableInfo(tableName,dbType)
    colName = jsonTable["columnName"]
    columnType = jsonTable["columnType"]
    primaryKey = jsonTable["primaryKey"]
    tableInfo.setColumnName(colName)
    tableInfo.setColumnType(columnType)
    tableInfo.setPrimaryKey(primaryKey)
    if "tableData" in jsonTable.keys():
        tableData = jsonTable["tableData"]
        rowNum = len(tableData)
        tableInfo.setTableData(tableData)
        tableInfo.setRowNum(rowNum)
    return tableInfo
    
def loadTableDataFromJson(fileName,tableInfo):
    with open(fileName,"r",encoding="utf-8") as fp:
        jsonTable = json.load(fp)
    if jsonTable == None:
        return None
    if "tableData" in jsonTable.keys():
        tableData = jsonTable["tableData"]
        rowNum = len(tableData)
        tableInfo.setTableData(tableData)
        tableInfo.setRowNum(rowNum)
    return


if __name__ == "__main__":
    fileName = './src/test.json'
    table = loadTableInfoFromJson(fileName)
    print(table)